package org.idea.spring.look.up.factory;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;


/**
 * 父类ioc容器 这里面的ioc容器只包含有ParentBean这个类
 *
 * @Author linhao
 * @Date created in 8:46 上午 2021/4/11
 */
public class ParentIocContainer {


    public static AnnotationConfigApplicationContext applicationContext = null;

    class ParentBean {
        int id;

        public ParentBean(){
            System.out.println("this is no arg init");
        }

        @Override
        public String toString() {
            return "ParentBean{" +
                    "id=" + id +
                    '}';
        }
    }

    public ApplicationContext getAndStartApplicationContext(){
       applicationContext = new AnnotationConfigApplicationContext();
       applicationContext.register(ParentIocContainer.class);
       //需要支持无参构造函数
       applicationContext.registerBean("parentBean",ParentBean.class);
       applicationContext.refresh();
       return applicationContext;
    }

    public static void main(String[] args) {
        ParentIocContainer parentIocContainer = new ParentIocContainer();
        ApplicationContext applicationContext = parentIocContainer.getAndStartApplicationContext();
        String[] str = applicationContext.getBeanNamesForType(ParentBean.class);
        for (String beanName : str) {
            System.out.println(beanName);
        }
    }


}
